package com.now.printer.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.now.printer.R;
import com.now.printer.base.BaseAdapter.BaseAdapter;
import com.now.printer.base.BaseAdapter.BaseHolder;
import com.now.printer.bean.FileBean;
import com.now.printer.db.DocumentsTable;
import com.now.printer.utils.FileUtils;

import org.xutils.common.util.DensityUtil;
import org.xutils.common.util.LogUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xudong on 2021/5/16.
 */
public class SearchListResultAdapter extends BaseAdapter<DocumentsTable> {
    private OnItemClickListener mOnItemClickListener = null;


    public SearchListResultAdapter(Context context, List<DocumentsTable> data, int layoutId) {
        super(context, data, layoutId);
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @Override
    public void onBind(BaseHolder holder, DocumentsTable item, int position) {
        TextView itemName = holder.getView(R.id.item_name);
        ImageView itemIcon = holder.getView(R.id.item_icon);
        RelativeLayout lr = holder.getView(R.id.lr);

        if (item != null) {
            itemName.setText(item.getName());
//            itemIcon.setImageResource(item.iconId);

            if (FileUtils.isPicFile(item.getName())) {
                x.image().bind(holder.getView(R.id.item_icon), new File(item.getPath()).toURI().toString(), new ImageOptions.Builder()
                        .setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120))
                        .setRadius(DensityUtil.dip2px(5))
                        // 如果ImageView的大小不是定义为wrap_content, 不要crop.
                        .setCrop(true) // 很多时候设置了合适的scaleType也不需要它.
                        // 加载中或错误图片的ScaleType
                        //.setPlaceholderScaleType(ImageView.ScaleType.MATRIX)
                        .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                        .setLoadingDrawableId(R.mipmap.ic_launcher)
                        .setFailureDrawableId(R.mipmap.ic_launcher)
                        .setIgnoreGif(false)
                        .build());
            } else {
                itemIcon.setImageResource(FileUtils.getFileIconByPath(item.getPath()));
            }
            lr.setOnClickListener(v -> {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(position, item.getName());
                }
            });
            lr.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
//                    //不是pdf格式， 因此直接分享到wps或者office的APP里去转换之后， 再通过三方分享回这里
//                    Intent intent1=new Intent(Intent.ACTION_SEND);
//                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    intent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//
////                    Uri uri = FileUtils.getFileUri(getActivity(),new File(searchResultList.get(position).getPath()));
////                    FileUtils2.getFileFromUri(getActivity(),Uri.parse(searchResultList.get(position).getPath()))
//                    intent1.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(item.getPath())) );
//                    String mimeType = FileUtils.getMIMEType(new File(item.getPath()));
//                    intent1.setType(mimeType);
//                    context.startActivity(Intent.createChooser(intent1,"使用其他应用打开"));
//                    LogUtil.i("getMimeType ： " + mimeType);
                    return true;
                }
            });
        }else {
            LogUtil.i("adapter item is null");
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position, String type);
    }
}

